package game.editer;

import hx.events.MouseEvent;
import game.editer.TimelineEditer.LabelsTip;
import hx.layout.AnchorLayoutData;
import hx.utils.ColorUtils;
import hx.geom.ColorTransform;
import hx.ui.UIManager;
import hx.display.Image;
import game.data.FrameData;
import game.editer.ui.Config;
import hx.display.Label;
import hx.display.Quad;
import hx.display.ItemRenderer;

/**
 * 时间轴的帧渲染对象组件
 */
class TimelineFrame extends ItemRenderer {
	public static var SELECTED_COLOR = new ColorTransform(1, 1, 0, 1, 0, 0, 0, 100);

	public var quad:Quad;

	public var label:Label;

	public var otherFrames:Array<Quad> = [];

	override function onInit() {
		super.onInit();

		var bg = new Quad(1, 1);
		bg.width = 14;
		bg.height = 26;
		this.addChild(bg);
		bg.alpha = 0;
		bg.y = 16;

		quad = new Quad(12, 26, 0x959ba3);
		quad.x = 1;
		quad.y = 16;
		this.addChild(quad);
		quad.addEventListener(MouseEvent.CLICK, onQuadClick);

		label = new Label(Config.FRAME_TEXT_FORMAT);
		this.addChild(label);
		label.width = 14;
		label.horizontalAlign = CENTER;
		label.verticalAlign = MIDDLE;
		label.height = 16;
		label.data = "1";
		label.y = -2;
		label.mouseEnabled = false;

		var len = LabelsTip.labels.length - 1;
		for (i in 0...len) {
			var quad = new Quad(12, 24, 0x959ba3);
			quad.x = 1;
			quad.y = 1 + 16 + 27 + 27 * i;
			this.addChild(quad);
			quad.alpha = 0.4;
			otherFrames.push(quad);
			quad.name = LabelsTip.labels[i + 1];
			quad.addEventListener(MouseEvent.CLICK, onQuadClick);
		}
	}

	private function onQuadClick(e:MouseEvent) {
		var quad:Quad = cast e.target;
		this.quad.colorTransform = null;
		for (q in otherFrames) {
			// 如果选择的是帧，则全选
			q.colorTransform = this.quad == quad ? SELECTED_COLOR : null;
		}
		quad.colorTransform = SELECTED_COLOR;
	}

	override function setSelected(selected:Bool) {
		super.setSelected(selected);
		if (selected) {
			for (quad in otherFrames) {
				quad.alpha = 0.9;
			}
			this.quad.data = 0x959ba3;
			this.quad.alpha = 0.9;
		} else {
			this.quad.colorTransform = null;
			for (quad in otherFrames) {
				quad.alpha = 0.4;
				quad.colorTransform = null;
			}
			this.quad.alpha = 0.4;
			if (this.data != null)
				this.quad.data = data.data.name == "" ? 0x4F4F4F : 0x959ba3;
		}
	}

	override function set_data(value:Dynamic):Dynamic {
		if (value != null) {
			var info:TimelineFrameData = value;
			if (info.frame % 2 == 0) {
				this.label.visible = true;
				this.label.data = Std.string(info.frame + 1);
			} else {
				this.label.visible = false;
			}
			this.quad.data = info.data.name == "" ? 0x4F4F4F : 0x959ba3;
			// 渲染是否存在特效
			otherFrames[0].visible = info.data.effects.length > 0;
			// 存在停顿帧
			otherFrames[1].visible = info.data.isStop;
			// 存在碰撞块
			otherFrames[8].visible = info.data.collision != null && info.data.collision.shapes.length > 0;
			// 存在位移
			otherFrames[2].visible = info.data.originMoveX != null || info.data.originMoveY != null;
			// 存在事件
			otherFrames[6].visible = info.data.eventTag != "" && info.data.eventTag != null;
			// 存在音频
			otherFrames[7].visible = info.data.sound != null && info.data.sound != "";
			// otherFrames[5].visible = otherFrames[4].visible = otherFrames[3].visible = false;
			// 存在缩放
			otherFrames[3].visible = info.data.originScaleX != null || info.data.originScaleY != null;
			// 存在旋转
			otherFrames[4].visible = info.data.originRotate != null;
			// 存在透明度
			otherFrames[5].visible = info.data.originAlpha != null;
		}
		return super.set_data(value);
	}
}

typedef TimelineFrameData = {
	var frame:Int;
	var data:FrameData;
}
